﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace PeisongWeb
{
    public partial class buyer_categoryCollect :buyerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DC", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "buyer_index.html");
                    return;
                }
            }
        }

        private void drpBind()
        {
            drp_CategoryRoot.DataSource = logic.category.getByParent(0);
            drp_CategoryRoot.DataBind();
            drp_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        void rptBind()
        {
            int companyId = logic.customer.getCompanyId();
            StringBuilder where = new StringBuilder();
            where.AppendFormat("companyId={0}", companyId);
            if (drp_CategoryRoot.SelectedValue.ToString().Trim() != "")
                where.AppendFormat("and parentId={0}", drp_CategoryRoot.SelectedValue.ToString().Trim());
            if (drp_CategorySub.SelectedValue.ToString().Trim() != "")
                where.AppendFormat("and categoryId={0}", drp_CategorySub.SelectedValue.ToString().Trim());

            DataSet ds = logic.category.listWithCollect(where.ToString(), "parentId", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            //rpt_list.DataSource = logic.category.listWithReport(where.ToString());
            rpt_list.DataBind();
        }
        void categorySubBind()
        {
            if (drp_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(drp_CategoryRoot.SelectedValue));
                drp_CategorySub.DataSource = dt;
                drp_CategorySub.DataBind();
                drp_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
            else
            {
                drp_CategorySub.Items.Clear();
            }
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
        }
        protected void btn_select_Click(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
    }
}
